package reduce.fz.com.reduce.fragment;


import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.StoryDetailActivity;
import reduce.fz.com.reduce.activity.TextRecycleActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.StoryCard;
import reduce.fz.com.reduce.bean.StoryPicCard;
import reduce.fz.com.reduce.bean.StoryReply;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.custom.delegate.RecyclerViewDelegate;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.event.PubStoryEvent;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ImageDisplay;
import reduce.fz.com.reduce.util.ScreenUtils;

/**
 * 故事卡片推荐流
 */
public class StoryRecommendFragment extends BaseListFragment<StoryRecommendFragment.StoryRecommendViewHolder, StoryCard> implements FragmentCacheSupport {
    private Team team;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    public StoryRecommendFragment() {
    }

    private RecyclerViewDelegate mRecyclerViewDelegate = new RecyclerViewDelegate();


    public boolean isViewBeingDragged(MotionEvent event) {
        return mRecyclerViewDelegate.isViewBeingDragged(event, recyclerView);
    }

    public static StoryRecommendFragment create(Team team) {
        StoryRecommendFragment fragment = new StoryRecommendFragment();
        Bundle bundle=new Bundle();
        bundle.putSerializable("team",team);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        team= (Team) getArguments().getSerializable("team");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    public void onEvent(PubStoryEvent event)
    {
        addFrist(event.card);
    }


    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回列表
        return null;
    }

    @Override
    public String getCacheName() {
        return null;
    }

    @Override
    public String getFragmentTitle() {
        return "他的历程";
    }

    public interface StoryRecommendListener {
        public void onRecommendClick(StoryCard card);
    }


    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<StoryCard>>(new TypeReference<Result<List<StoryCard>>>() {
        }) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<List<StoryCard>> data) {
                addResult(data);
            }
        });
    }

    @Override
    protected StoryRecommendViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_story_recommend_card, parent, false);
        StoryRecommendViewHolder holder = new StoryRecommendViewHolder(getActivity(), view, new StoryRecommendListener() {
            @Override
            public void onRecommendClick(StoryCard card) {
                Intent intent = new Intent(context, StoryDetailActivity.class);
                intent.putExtra("storyid", card.storyid);
                startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    protected StoryRecommendViewHolder createfootViewHolder(View view) {
        return new StoryRecommendViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<StoryCard> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_story_recommend, team.teamid,pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return Pagination.TYPE_START_NUM;
    }


    public static class StoryRecommendViewHolder extends BaseRecycleViewHolder<StoryCard> {
        private TextView mTvTitle;
        private NetworkImageView recommend1;
        private NetworkImageView recommend2;
        private NetworkImageView recommend3;
        private LinearLayout mllrecommends;
        private TextView mNickName;
        private RoundNetworkImageView mAvatar;
        private TextView mPubTime;
        private StoryRecommendListener storyRecommendListener;
        private View itemView;
        private LinearLayout.LayoutParams params;
        private Activity mActivity;

        private int paddingLeft = context.getResources().getDimensionPixelSize(R.dimen.dimen_15);
        private int paddingRight = context.getResources().getDimensionPixelSize(R.dimen.dimen_15);
        private int margincenter = context.getResources().getDimensionPixelSize(R.dimen.dimen_15);
        private int height = 0;
        private List<String> imageList;

        public StoryRecommendViewHolder(Activity activity, View itemView, StoryRecommendListener listener) {
            this(itemView);
            this.itemView = itemView;
            this.mActivity = activity;
            this.storyRecommendListener = listener;
        }

        public List<String> getImageList(StoryCard card) {
            List<String> v = new ArrayList<String>();
            List<StoryPicCard> cards = card.picCards;
            if(cards==null||cards.isEmpty()) return null;
            for (int i = 0; i < cards.size(); i++) {
                v.add(cards.get(i).cardurl.url);
            }
            return v;
        }

        public StoryRecommendViewHolder(View itemView) {
            super(itemView);
            mAvatar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_avatar);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }

        public void toImageDispaly(View view, final int position) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageDisplay.toDisplay(mActivity, (ArrayList<String>) imageList, position);
                }
            });
        }


        public void setHeight(View view, int marginleft, int margintop, int marginright, int marginbottom) {
            if (view == null) return;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(height, height);
            params.setMargins(marginleft, margintop, marginright, marginbottom);
            view.setLayoutParams(params);
        }

        public int calHeight() {
            int width = ScreenUtils.getScreenW();
            int length = width - 2 * margincenter - paddingLeft - margincenter;
            length = length / 3;
            return length;
        }

        @Override
        public void renderView(final StoryCard data, int position) {
            if(data==null) return;
            if(data.user!=null)
            mAvatar.setImageUrl(data.user.avatar, HttpUtils.getAvatarImageLoader());
            mTvTitle.setText(data.title);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    storyRecommendListener.onRecommendClick(data);

                }
            });

        }
    }


}
